package com.peas.platform.module.icims.contract.initial.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.peas.platform.framework.mybatisplus.domain.BaseDomain;
import com.peas.platform.framework.mybatisplus.po.BasePo;
import com.peas.platform.framework.mybatisplus.service.BaseServiceImpl;
import com.peas.platform.module.icims.base.approve.component.ApproveCallback;
import com.peas.platform.module.icims.base.approve.dto.ApproveDto;
import com.peas.platform.module.icims.base.approve.dto.ApproveOrderNodeDto;
import com.peas.platform.module.icims.base.approve.enums.ApproveOrderBusinessType;
import com.peas.platform.module.icims.base.approve.service.ApproveOrderService;
import com.peas.platform.module.icims.base.approve.service.ApproveService;
import com.peas.platform.module.icims.contract.initial.controller.vo.ContractInitialSupplementalVo;
import com.peas.platform.module.icims.contract.initial.domain.ContractInitialSupplementalDomain;
import com.peas.platform.module.icims.contract.initial.dto.ContractInitialDto;
import com.peas.platform.module.icims.contract.initial.dto.ContractInitialSupplementalAttachmentDto;
import com.peas.platform.module.icims.contract.initial.dto.ContractInitialSupplementalDto;
import com.peas.platform.module.icims.contract.initial.enums.ContractInitialSupplementalState;
import com.peas.platform.module.icims.contract.initial.service.ContractInitialSupplementalAttachmentService;
import com.peas.platform.module.icims.contract.initial.service.ContractInitialSupplementalService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ContractInitialSupplementalServiceImpl extends BaseServiceImpl<ContractInitialSupplementalDto> implements ContractInitialSupplementalService, ApproveCallback {

    @Resource
    private ContractInitialSupplementalDomain contractInitialSupplementalDomain;
    @Resource
    private ContractInitialSupplementalAttachmentService contractInitialSupplementalAttachmentService;
    @Resource
    private ApproveService approveService;
    @Resource
    private ApproveOrderService approveOrderService;

    @Override
    public BaseDomain<ContractInitialSupplementalDto, ? extends BasePo> getDomain() {
        return contractInitialSupplementalDomain;
    }

    @Transactional
    @Override
    public void supplementalContractInitial(ContractInitialDto dto, Long approveId, List<ApproveOrderNodeDto> approveOrderNodes, List<ContractInitialSupplementalVo> items) {
        ApproveDto ad = approveService.getById(approveId);
        ApproveOrderBusinessType approveOrderBusinessType = ApproveOrderBusinessType.contractSupplemental;
        Assert.notNull(approveOrderBusinessType, "合同数据错误");
        items.forEach(v -> {
            ContractInitialSupplementalDto item = BeanUtil.copyProperties(v, ContractInitialSupplementalDto.class);
            item.setState(ContractInitialSupplementalState.submit.getKey());
            this.save(item);
            List<ContractInitialSupplementalAttachmentDto> attachments = BeanUtil.copyToList(v.getAttachments(), ContractInitialSupplementalAttachmentDto.class);
            attachments.forEach(m -> m.setContractInitialSupplementalId(item.getId()));
            contractInitialSupplementalAttachmentService.saveBatch(attachments);
            String desc = "合同编码(" + dto.getCode() + ")";
            approveOrderService.createApproveOrder(ad, approveOrderNodes, approveOrderBusinessType, item.getId(), desc, null, this.getClass());
        });
    }

    @Transactional
    @Override
    public void revocationContractInitialSupplemental(Long id) {
        ContractInitialSupplementalDto dto = this.getById(id);
        Assert.notNull(dto, "找不到数据");
        Assert.equals(dto.getState(), ContractInitialSupplementalState.submit.getKey(), "单据状态不是[{}]", ContractInitialSupplementalState.submit.getValue());
        dto.setState(ContractInitialSupplementalState.revocation.getKey());
        this.updateById(dto);
        ApproveOrderBusinessType approveOrderBusinessType = ApproveOrderBusinessType.contractSupplemental;
        Assert.notNull(approveOrderBusinessType, "合同数据错误");
        approveOrderService.revocationApproveOrder(approveOrderBusinessType, id);
    }

    @Transactional
    @Override
    public void callbackAfterApprove(Long businessId, boolean isPass, boolean isFinalPass) {
        if (businessId == null)
            return;
        if (isPass) {
            if (isFinalPass) {
                ContractInitialSupplementalDto dto = this.getById(businessId);
                dto.setState(ContractInitialSupplementalState.pass.getKey());
                this.updateById(dto);
            }
        } else {
            ContractInitialSupplementalDto dto = this.getById(businessId);
            dto.setState(ContractInitialSupplementalState.reject.getKey());
            this.updateById(dto);
        }
    }
}
